package com.fengpt.mysqlback.service;

import com.fengpt.mysqlback.util.FileUtils;
import com.fengpt.mysqlback.util.SqlUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

//@Service
public class BackUpDoServiceImpl {

    //@Value("${spring.datasource.driverClassName}")
    private String jdbcDriver="com.mysql.jdbc.Driver";

    //@Value("${spring.datasource.druid.first.url}")
    private String jdbcUrl="jdbc:mysql://rm-2zeyi4pfw4y510bevao.mysql.rds.aliyuncs.com:3306/halodb?characterEncoding=UTF-8";

    //@Value("${spring.datasource.druid.first.username}")
    private String jdbcUserName="fengpt";

    //@Value("${spring.datasource.druid.first.password}")
    private String jdbcPassword="1107080230Fpt";

    //@Value("${spring.basePath}")
    private String basePath="d:/";

    //@Value("${spring.dbName}")
    private String dbName="halodb";

    public static void main(String[] args) throws Exception{
        BackUpDoServiceImpl backUpDoServiceImpl=new BackUpDoServiceImpl();
        backUpDoServiceImpl.backupData();
    }

    public void backupData() throws IOException, ClassNotFoundException, SQLException {
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdir();
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String now = df.format(LocalDateTime.now());
        File dataFile = null;
        Class.forName(jdbcDriver);
        Connection con = DriverManager.getConnection(jdbcUrl, jdbcUserName, jdbcPassword);
        Statement statement = con.createStatement();
        DatabaseMetaData dbmd = con.getMetaData();
        con.getMetaData().getDatabaseProductName();
        ResultSet rs = dbmd.getTables(dbName, null, null, new String[]{"TABLE"});
        List<String> tableNames = new ArrayList<>();
        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            tableNames.add(tableName);
        }
        if (tableNames != null && tableNames.size() > 0) {
            dataFile = new File(basePath + "/" + now + "mysql数据" + ".sql");
            if (!dataFile.exists()) {
                /* 创建文件*/
                dataFile.createNewFile();
            }
            FileWriter dataFw = new FileWriter(dataFile, true);
            for (String str : tableNames) {
                StringBuilder datWt = new StringBuilder();
                try {
                    String insertSqlStr = SqlUtils.getMysqlInsertSql(statement, str, "");
                    dataFw.write(insertSqlStr);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            dataFw.flush();
            dataFw.close();
        }
        rs.close();
        statement.close();
        con.close();
     /*   *//** 压缩方法2  *//*
        List<File> fileList = new ArrayList<>();
        if (dataFile != null) {
            fileList.add(dataFile);
        }
        FileOutputStream fos2 = new FileOutputStream(new File(basePath + f.format(date) + "mysql数据" + ".zip"));
        try {
            FileUtils.toZip(fileList, fos2);
            dataFile.delete();
            fos2.close();
        } catch (Exception ex) {

        }*/
    }
}
